﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using AllInOnePluginBL.Helpers;
using Rss;

namespace AllInOnePluginBL.SiteScrappers
{
    /// <summary>
    /// Summary description for VipmovieNetParser
    /// </summary>
    public class VipmovieNetParser
    {
        private enum SearchMode
        {
            s,  //Series
            m   //Movies
        }

        protected const string DEFAULT_URL = "http://vipmovie.net";
        private const string SEARCH_URL = "http://vipmovie.net/?act=search&terms={0}&t={1}";
        private const SearchMode _searchMode = SearchMode.m;

        private readonly Encoding _pageEcoding = Encoding.GetEncoding("windows-1255");

        public VipmovieNetParser()
        {
         //   this.Name = "VipMovie.Net";
        }

        #region Private Methods
        private RssFeed LoadMovies(string url)
        {
            try
            {
                var page = Utils.GetPage(url, this._pageEcoding);

                const string moviesContainerExpression = "<div class=\"MOVIESimg\"><img src=\"(.+?)\" class=\"img\" alt=\"(.+?)\".+?<div class=\"MOVIEStxt_title\"><a.*?href=\"(.+?)\"><b>(.+?)</b></a>.+?<div class=\"MOVIEStxt_info\">(.*?)</div>";
                var exp = new Regex(moviesContainerExpression, RegexOptions.Singleline);
                var matches = exp.Matches(page);
                foreach (Match match in matches)
                {
                    var movieImage = match.Groups[1].Value;
                    var movieTitle = match.Groups[2].Value;
                    var moviePageLink = DEFAULT_URL + match.Groups[3].Value;

                    //var movie = new Movie
                    //                {
                    //                    Name = movieTitle,
                    //                    ImageLink = movieImage,
                    //                };

                    //var pageContent = Utils.GetPage(moviePageLink, this._pageEcoding);
                    //movie.Plot = GetPlot(pageContent, movieTitle);
                    //movie.MovieLinks = GetMovieLinks(pageContent);

                    //if (movie.MovieLinks.Count > 0)
                    //{
                    //    var movieItems = movie.GetRssItems(Utils.GetSiteUrl(), this._pluginId,
                    //                                       (int) PluginMode.MOVIE_MODE);
                    //    foreach (var movieItem in movieItems)
                    //    {
                    //        rssChannel.Items.Add(movieItem);
                    //    }
                    //}
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private string GetNextPageLink(string pageContent)
        {
            try
            {
                const string moviePlotExpression = "class=\"MOVIESpaging\">.*?class=\"current\">.*?<a href=\"(.+?)\"";
                var exp = new Regex(moviePlotExpression, RegexOptions.Singleline);
                var match = exp.Match(pageContent);
                if (match.Success)
                {
                    return string.Format("{0}/{1}", DEFAULT_URL, match.Groups[1].Value.Trim());
                }
            }
            catch { }

            return string.Format("{0}/p{1}#start_movies_list", DEFAULT_URL, 1);
        }

        private string GetPlot(string pageContent, string movieTitle)
        {
            try
            {
                var moviePlotExpression = "class=\"SWITCH_BOXbg\">.*?<b>" + movieTitle + "</b> <span>|</span>.*?</b><br />(.+?)<br><br>.*?<center>";
                var exp = new Regex(moviePlotExpression, RegexOptions.Singleline);
                var match = exp.Match(pageContent);
                if (match.Success)
                {
                    return match.Groups[1].Value.Trim();
                }
            }
            catch{}

            return string.Empty;
        }
        #endregion
    }
}